package spamer;


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

import sc.helper.Constants;
import sc.helper.Helper;

public class UDPSender implements Runnable {

	private final static int PORT = 6111;
	
	private DatagramSocket socket;
	
	private boolean isRunning = true;
	
	public UDPSender() {
		try {
			socket = new DatagramSocket(null);
			socket.setReuseAddress(true);
			socket.bind(new InetSocketAddress(6112));
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		while(isRunning) {
			try {

				// Create new game
				byte[] data = Helper.hexStringToByteArray("040956000000000050584553D10000000000000061645573002C34332C31322C362C312C662C2C312C35616235343338622C312C2C616455730D41737472616C2042616C616E63650D000C0000000100000002000000");
				
				// Game listen broadcast
				//byte[] data = Helper.hexStringToByteArray(C31314000200000050584553D100000000000000");
				
				DatagramPacket packet = new DatagramPacket(data, data.length, new InetSocketAddress(Constants.UDP_RECEIVE_HOST, PORT));
				socket.send(packet);
				
				System.out.println("Spamer sent: " + Helper.byteArrayToHexString(data) );

				isRunning = false;
				
				Thread.sleep(1000);
				
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public synchronized void shutdown() {
		isRunning = false;
	}
}
